package onlineclass.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import onlineclass.entity.OMajor;
import onlineclass.entity.OUser;
import onlineclass.service.OMajorService;
import onlineclass.service.OUserService;
import onlineclass.util.AjaxPage;
import onlineclass.util.AjaxResult;
import onlineclass.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OMajorController {
    public void find(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String name = request.getParameter("name");
        request.setAttribute("name",StringUtil.trim(name));
        AjaxPage ajaxPage=new AjaxPage();
        List<OMajor> list = OMajorService.INST.find(ajaxPage.getPage(),ajaxPage.getLimit(),name);
        AjaxResult success;
        if (list!=null){
            success= AjaxResult.success(list);
        }else {
            success= AjaxResult.error("错误");
        }
        String jsonString = JSON.toJSONStringWithDateFormat(success,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().println(jsonString);

    }

    public void add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String name = request.getParameter("name");
        int inuse = StringUtil.toInt(request.getParameter("inuse")) ;
        String description = request.getParameter("description");
        request.setAttribute("description",StringUtil.trim(description));
        OMajor s = new OMajor(name, inuse, description);
        int add = OMajorService.INST.add(s);
        AjaxResult success;
        if (add>=1){
            success= AjaxResult.success(add);
        }else {
            success= AjaxResult.error("错误");
        }
        String jsonString = JSON.toJSONString(success);
        response.getWriter().print(jsonString);
    }

    public void get(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = StringUtil.toLong(request.getParameter("id"));
        OMajor oMajor = OMajorService.INST.get(id);
        AjaxResult success;
        if (oMajor!=null){
            success= AjaxResult.success(oMajor);
        }else {
            success= AjaxResult.error("错误");
        }
        String jsonString = JSON.toJSONStringWithDateFormat(success,"yyyy-MM-dd", SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNonStringValueAsString);
        response.getWriter().println(jsonString);
    }

    public void update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = StringUtil.toLong(request.getParameter("id"));
        String name = request.getParameter("name");
        int inuse = StringUtil.toInt(request.getParameter("inuse"),1) ;
        String description = request.getParameter("description");
        OMajor s = new OMajor(id,name, inuse, description);
        int update = OMajorService.INST.update(s);
        AjaxResult success;
        if (update>=1){
            success= AjaxResult.success(update);
        }else {
            success= AjaxResult.error("错误");
        }
        String jsonString = JSON.toJSONString(success);
        response.getWriter().println(jsonString);
    }

    public void del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = StringUtil.toLong(request.getParameter("id"));
        int del = OMajorService.INST.del(id);
        AjaxResult success;
        if (del>=1){
            success= AjaxResult.success(del);
        }else {
            success= AjaxResult.error("错误");
        }
        String jsonString = JSON.toJSONString(success);
        response.getWriter().println(jsonString);
    }
}
